/*HDU 1213 How Many Tables*/
#include <cstdio>
class UnionFinder
{
public:
    UnionFinder (int n) {
        p = new int[n+1];
        size = new int[n+1];
        cnt = n;
        for (int i = 0; i <= n; ++i) {
            p[i] = i;
            size[i] = 1;
        }
    };

    virtual ~UnionFinder (){delete [] p; delete [] size;};

    void unite(int a, int b) {
        int x = find(a);
        int y = find(b);
        if (x != y) {
            if(size[x] > size[y]) {
                p[y] = x;
                size[x] += size[y];
            } else {
                p[x] = y;
                size[y] += size[x];
            }
            cnt--;
        }
    }

    int find(int a) {
        int k = a;
        int i;
        while(a != p[a]) a = p[a];
        while(p[k] != a) {
            i = p[k];
            p[k] = a;
            k = i;
        }
        return a;
    }
    int count() {return cnt;}

private:
    int *p, *size;
    int cnt;
};

int main(int argc, char *argv[])
{
    int numOfTestCases;
    int N,M;
    scanf("%d",&numOfTestCases);
    for (int i = 0; i < numOfTestCases; ++i) {
        scanf("%d %d",&N,&M);
        UnionFinder uf(N);
        int a, b;
        for (int j = 0; j < M; ++j) {
            scanf("%d %d",&a, &b);
            uf.unite(a,b);
        }
        printf("%d\n", uf.count());
    }
    return 0;
}
